﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Win32;

namespace iSmartCreatorApp
{
    /// <summary>
    /// Interaction logic for LinkAddWindow.xaml
    /// </summary>
    public partial class LinkAddWindow : Window
    {
        //Constructor
        public LinkAddWindow()
        {
            InitializeComponent();
            btnOk.IsEnabled = false;
            txtbxLinkLocation.Focus();
        }

        //Button "Cancel" click
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        //Button "OK" click
        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            Paragraph paragraph = new Paragraph();
            FlowDocument flow = new FlowDocument();
            ArrayList list = new ArrayList();
            string linkName;

            linkName = txtbxLinkText.Text == "" ? txtbxLinkLocation.Text : txtbxLinkText.Text;
            string[] mas = MainWindow.HtmlText.Split('\n');
            string body = FindBody(mas);
            body += "<a href=\"" + txtbxLinkLocation.Text + "\">" + linkName + "</a>";

            //MainWindow.currentRTBHtml.Document.Blocks.Clear();

            foreach (var i in mas)
            {
                list.Add(i);
            }
            list = ClearBody(list);
            list = NewBody(list, body);
            mas = (string[]) list.ToArray(typeof (string));
            mas = MainWindow.GoldClear(mas);
            foreach (var i in mas)
            {
                paragraph.Inlines.Add(i + "\n");
            }
            flow.Blocks.Add(paragraph);
            MainWindow.currentRTBHtml.Document = flow;
            this.Close();
        }

        //Enable button "OK" if text != empty
        private void txtbxLinkLocation_TextChanged(object sender, TextChangedEventArgs e)
        {
            btnOk.IsEnabled = txtbxLinkLocation.Text != "";
        }

        //Button to search file
        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            var openFile = new OpenFileDialog {Title = @"Choose some file"};
            var result = openFile.ShowDialog();

            if (result != true) return;

            txtbxLinkLocation.Text = openFile.FileName;
        }

        //Find Body
        private string FindBody(string[] mas)
        {
            string str = "";

            for (int i = 0; i < mas.Length; i++)
            {
                if (mas[i] == "<body>")
                {
                    i++;
                    while (mas[i] != "</body>")
                    {
                        str += mas[i] + "\n";
                        i++;
                    }
                    break;
                }
            }
            return str;
        }

        //Clear Body
        private ArrayList ClearBody(ArrayList list)
        {
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].ToString() == "<body>")
                {
                    i++;
                    while (list[i].ToString() != "</body>")
                    {
                        list.Remove(list[i]);
                    }
                    break;
                }
            }
            return list;
        }       

        //New Body
        private ArrayList NewBody(ArrayList list, string body)
        {
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].ToString() == "<body>")
                {
                    list.Insert(i + 1, body);
                    break;
                }
            }
            return list;
        }
    }
}